<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Musique Basse</title>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
        <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
        <script type="text/javascript" src="js/jquery.min.js"></script>
    </head>
    <body>
        <div id="main" class="page">
            <header>
                <h1><img src="buttons/logo.png"/></h1>
                <h6>A music store for all kinds of music listeners</h6>
                <nav>
                    <a href="index.php"><img src="buttons/home.png"/></a>
                    <a href="products.php"><img src="buttons/products.png"/></a>
                    <!--accessible or shown only to logged in users-->
                    <?php //var_dump($_SESSION);
                        
                            echo '<a href="account.php"><img src="buttons/account.png"/></a>';
							echo '<a href="cart.php"><img src="buttons/Cart.png"/></a>';
                            echo '<a href="logout.php" class="fltright"><img src="buttons/logout.png"/></a>';
                        
                    ?>
                </nav>
            </header>
            
            <div id="content">
			
                <?php
	error_reporting(0);
	$indexpath = "index.php";
	$purchasedpath = "data/purchased.xml";
	require_once "data/SessionControl.php";
	require_once "data/cartfunctions.php";
	require_once "createcart.php";
	require_once "checkout.php";
	
	$album = $_GET["albumid"];
	$track = $_GET["trackno"];
	$action = $_GET["action"];
	$back = 'album.php?album='.$album;
	//$cart = new cart();
	// insert checker here (if the same song is added to cart

	/*session_start();	
	if(!isset($_SESSION["isLoggedIn"]) || !($_SESSION["isLoggedIn"] === true)){
        $songArray = array();
		$_SESSION['songs'] = $songArray;
    }*/
	
	switch($action) {	//decide what to do	
		case "add":
			$checker1=0;
			$checker2=0;
			foreach($_SESSION['songs'] as $key=>$value){
				if( ($_SESSION['songs'][$key]->getAlbumID()==$album) && ($_SESSION['songs'][$key]->getTrackNo()==$track) ){
					$checker1=1;
					echo "Track already exists in cart.<br/>";
					break;
				}
			}
			$checker2=checkTrack($album, $track);
			
			if($checker2==1)
				echo "Track already purchased by this account.<br/>";
			if( $checker1==0 && $checker2==0){
				$dom = new DOMDocument();
				$dom->load('products.xml');
				$xpath = new DOMXPath($dom);
				$result = 
				$song = new cartSong($album, $track);
				$_SESSION['songs'][]=$song;
				echo "Track added to cart.<br/>";
			}
			
			echo '<a href="'.$back.'">Continue Shopping</a>';
			//header('location: '.$GLOBALS['back']);
		break;
		
		case "remove":
			$checker=0;
			
			for($x=0;$x<count($_SESSION['songs']);$x++){
				if( ($_SESSION['songs'][$x]->getAlbumID()==$album) && ($_SESSION['songs'][$x]->getTrackNo()==$track) ){
					$checker=1;
					break;
				}
				//echo 'sessionAlbum='.$_SESSION['songs'][$x]->getAlbumID().'<br/>album='.$album.'<br/>';
				//echo 'sessionSong='.$_SESSION['songs'][$x]->getTrackNo().'<br/>song='.$track.'<br/>';
				//echo 'checker='.$checker.'<br/>';
			}
			if($checker==1){
				//$song = new cartSong($album, $track);				
				$temp = $_SESSION['songs'];
				if($x==0 && count($_SESSION['songs'])>1){
					$tempsong = $_SESSION['songs'][$x];
					$tempsong2 = $_SESSION['songs'][count($_SESSION['songs'])-1];
					$_SESSION['songs'][$x] = $tempsong2;
					$_SESSION['songs'][count($_SESSION['songs'])-1] = $tempsong;
					$x=1;
				}
				//echo $x;
				unset($temp[count($_SESSION['songs'])-1]);
				//var_dump($temp);
				$_SESSION['songs']=$temp;
				echo "Track removed from cart.<br/>";
			}		
			echo '<a href="cart.php">Back to Cart</a>';
		break;
		
		case "empty":
			$eraser = array();
			$_SESSION['songs'] = $eraser;
			echo '<script language="javascript">alert("All cart items removed")</script>';
		break;
		
		case "checkout":
			$doc = new DOMDocument();
			$doc->load('data/products.xml');
			$xpath = new DOMXPath($doc);
			
				//$id = $_SESSION['songs'][0]->getAlbumID();
				//$trackno = $_SESSION['songs'][0]->getTrackNo();
				//echo $id;
				//echo $trackno;
				
			for($h=0;$h<count($_SESSION['songs']);$h++){
				$id = $_SESSION['songs'][$h]->getAlbumID();
				$trackno = $_SESSION['songs'][$h]->getTrackNo();
				//echo $id;
				//echo $trackno;
				$song = $xpath->query('/albums/album[id='.$id.']/songs/song[trackno='.$trackno.']');
				//var_dump($song);
				$song->item(0)->setAttribute('hits',$song->item(0)->getAttribute('hits')+1);
				$song->item(0)->getAttribute('hits');
				
			}
			$doc->save('data/products.xml');
			
			checkout();
			$eraser = array();
			$_SESSION['songs'] = $eraser;
			echo '<script language="javascript">alert("All cart items have now been purchased.")</script>';
			echo '<a href="products.php">Continue Shopping</a>';
		break;
	
	}
?>
            </div>
            
            <div class="clear"></div>
            
            <footer>
                <p>Copyright &copy; 2012 KENNICS. All rights reserved.</p>
                <p>Design by Werdnazner</p>
            </footer>
        </div>
    </body>
</html>